#include "sessionfriendarea.h"
#include"./modle/data.h"
#include"debug.h"
SessionFriendArea::SessionFriendArea(QWidget *parent):QScrollArea(parent)
{
    //初始化我们的滚动条
    // 设置滚动区域基本属性
    //对滚动条进行样式设置
    //设置这个才会进行滚动
    this->setAlignment(Qt::AlignTop);
    this->setWidgetResizable(true);
    // 初始化滚动条
    this->verticalScrollBar()->setStyleSheet(
        "QScrollBar:vertical {"
        "   width: 8px;"
        "   background: rgba(255, 255, 255, 0.05);"
        "   border-radius: 4px;"
        "   margin: 0px;"
        "}"
        "QScrollBar::handle:vertical {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff6b6b, stop:1 #06d6a0);"
        "   border-radius: 4px;"
        "   min-height: 30px;"
        "}"
        "QScrollBar::handle:vertical:hover {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff8e8e, stop:1 #08e6b0);"
        "}"
        "QScrollBar::handle:vertical:pressed {"
        "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
        "               stop:0 #ff4b4b, stop:1 #04c690);"
        "}"
        "QScrollBar::add-line:vertical, QScrollBar::sub-line:vertical {"
        "   border: none;"
        "   background: none;"
        "   height: 0px;"
        "}"
        "QScrollBar::add-page:vertical, QScrollBar::sub-page:vertical {"
        "   background: none;"
        "}");
    this->horizontalScrollBar()->setStyleSheet("QScrollBar:horizontal { height: 0px; }");
    //设置我们会会话展示区域的背景透明
    this->setStyleSheet("QWidget { border: none;}");
    contains=new QWidget(this);
    //重要必须进行设置
    this->setWidget(contains);

    QVBoxLayout * layout = new QVBoxLayout();
    layout->setContentsMargins(0,5,0,5);
    layout->setSpacing(2);
    contains->setLayout(layout);
#if TEST_UI
    //LOG() << "开始创建测试数据...";
    for(int i = 0; i < 10; i++)
    {
        QPushButton *avatar = new QPushButton();
        QString name = "好友" + QString::number(i+1);
        QString lastMsg = i % 3 == 0 ? "你好，最近怎么样？" :
                              i % 3 == 1 ? "[图片]" : "明天见面聊";
        addItem(SESSION_ITEM, avatar, name, lastMsg);
    }

    // 创建测试好友项
    for(int i = 0; i < 8; i++)
    {
        QPushButton *avatar = new QPushButton();
        QString name = "朋友" + QString::number(i+1);
        QString signature = i % 2 == 0 ? "生活不止眼前的苟且" : "努力奋斗中";
        addItem(FRIEND_ITEM, avatar, name, signature);
    }

    // 创建测试申请项
    for(int i = 0; i < 3; i++)
    {
        QPushButton *avatar = new QPushButton();
        QString name = "申请人" + QString::number(i+1);
        addItem(APPLY_ITEM, avatar, name, "");
    }
    //LOG() << "测试数据创建完成";
#endif

}

void SessionFriendArea::addItem(ITEM_TYPE type, QPushButton *avatar, QString username, QString text)
{
    SessionFriendItem*item=nullptr;
    switch(type)
    {
    case ITEM_TYPE::FRIEND_ITEM:
        item=new FriendItem(this,avatar,username,text);
        break;
    case ITEM_TYPE::SESSION_ITEM:
        item=new SessionItem(this,avatar,username,text);
        break;
    case ITEM_TYPE::APPLY_ITEM:
        item =new ApplyItem(this,avatar,username);
        break;
    default:
        LOG()<<"不正确的类型";
        break;
    }
    contains->layout()->addWidget(item);
}



void SessionFriendArea::ClearOtherSelected(SessionFriendItem *curentitem)
{
    QLayout *lay=contains->layout();
    for(int i=0;i<lay->count();i++)
    {
        QWidget *itemwidget=lay->itemAt(i)->widget();
        if(itemwidget==nullptr)continue;
        SessionFriendItem*item=dynamic_cast<SessionFriendItem*>(itemwidget);
        if(item==nullptr||item==curentitem)continue;
        if(item->isSelected())
        {
            item->setSelected(false);
            item->updateStyle();
        }
    }
}




SessionFriendItem::SessionFriendItem(SessionFriendArea *owner, QPushButton *avator, QString username, QWidget *parent)
    :selected(false),
    pressed(false),
    hovered(false),
    _owner(owner),
    _avator(avator),
    _username(username),
    QWidget(parent)
{
    layout=new QGridLayout();
    layout->setSpacing(5);
    layout->setContentsMargins(0,0,0,0);
    this->setLayout(layout);

    //设置我们的整个元素的尺寸策略
    this->setFixedHeight(70);

    this->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Fixed);

    //设置我们图像的大小
    _avator->setIcon(QIcon(":/Resources/avator1.jpg"));
    _avator->setFixedSize(45,45);
    _avator->setIconSize(QSize(40,40));
    // 设置头像样式
    // 设置头像样式 - 修改这一部分
    _avator->setStyleSheet(
        "QPushButton {"
        "   border: none;"
        "   border-radius: 10px;"
        "   background: qlineargradient(135deg, #ff6b6b, #ffa36c);"
        "}"
        "QPushButton:hover {"
        "   background: qlineargradient(135deg, #ff8e8e, #ffb78c);"
        "   border: 2px solid rgba(255, 255, 255, 0.3);"
        "}");

    //添加我们的姓名
    showname=new QLabel();
    showname->setText(username);
    //设计我们名字的样式
    // 优化名字样式
    showname->setStyleSheet(
        "QLabel {"
        "   color: rgb(220,220,220);"     // 亮灰色文字
        "   font-size: 14px;"             // 适当字体大小
        "   font-weight: 600;"            // 适中字重
        "   background: transparent;"     // 透明背景
        "   padding: 0px;"                // 无内边距
        "}"
        );

    // 设置文本对齐和自动换行
    showname->setAlignment(Qt::AlignLeft | Qt::AlignVCenter);
    showname->setWordWrap(false); // 不换行，显示省略号


    //下面就是加入到我们的布局管理器中
    layout->addWidget(_avator,0,0,2,2);
    layout->addWidget(showname,0,2,1,8);
}



//该事件只要是显示或者被遮盖后显示都会调用该事件 这里重写这个函数
//就是为了防止我们自定义的类我们在进行设置样式的时候不生效
//在Qt官方文档可以找到
void SessionFriendItem::paintEvent(QPaintEvent *event)
{
    (void) event;
    QStyleOption opt;
    opt.initFrom(this);
    QPainter p(this);
    style()->drawPrimitive(QStyle::PE_Widget, &opt, &p, this);
}

//鼠标按下事件
void SessionFriendItem::mousePressEvent(QMouseEvent *event)
{
    if(isSelected())return;
    (void)event;
    pressed=true;
    updateStyle();
    //设置我们此时的状态为选中状态
    pressed=false;
    selected=true;
    hovered=false;

    updateStyle();
    _owner->ClearOtherSelected(this);
    //执行之后点击之后逻辑
    //通过多态实现不同类的点击逻辑
    this->active();
}
//鼠标进入事件
void SessionFriendItem::enterEvent(QEnterEvent *event)
{
    //鼠标进入事件我们要修改样式
    if(selected==true) return;
    hovered = true;
    updateStyle();

}
//鼠标离开事件
void SessionFriendItem::leaveEvent(QEvent *event)
{
    if(selected==true) return;
    pressed = false;
    hovered=false;
    updateStyle();
}




void SessionFriendItem::active()
{
    LOG()<<"进入了列表点击";
}
void SessionFriendItem::updateStyle()
{
    QString style;
    if (selected) {
        // 选中状态 - 专业蓝色主题
        style =
            "SessionFriendItem {"
            "   background: qlineargradient(x1:0, y1:0, x2:1, y2:0,"
            "               stop:0 rgba(6, 214, 160, 0.2),"
            "               stop:0.5 rgba(255, 209, 102, 0.1),"
            "               stop:1 rgba(255, 107, 107, 0.1));"
            "   border: none;"
            "   border-left: 4px solid qlineargradient(x1:0, y1:0, x2:0, y2:1,"
            "                       stop:0 #ff6b6b, stop:1 #06d6a0);"
            "   border-radius: 0px 12px 12px 0px;"
            "}"
            "QLabel {"
            "   color: #ffd166;"
            "   background: transparent;"
            "   font-weight: bold;"
            "}";
    } else if (pressed) {
        // 按下状态 - 深色反馈
        style =
            "SessionFriendItem {"
            "   background: rgba(255, 255, 255, 0.15);"
            "   border: none;"
            "   border-left: 4px solid rgba(255, 255, 255, 0.3);"
            "   border-radius: 0px 12px 12px 0px;"
            "}"
            "QLabel {"
            "   color: #ffffff;"
            "   background: transparent;"
            "}";
    } else if (hovered) {
        // 悬停状态 - 温和的高亮
        style =
            "SessionFriendItem {"
            "   background: rgba(255, 255, 255, 0.08);"
            "   border: none;"
            "   border-left: 4px solid rgba(255, 255, 255, 0.2);"
            "   border-radius: 0px 12px 12px 0px;"
            "}"
            "QLabel {"
            "   color: #e8e8e8;"
            "   background: transparent;"
            "}";
    } else {
        // 普通状态 - 简洁现代
        style =
            "SessionFriendItem {"
            "   background: transparent;"
            "   border: none;"
            "   border-left: 4px solid transparent;"
            "}"
            "QLabel {"
            "   color: #c0c0c0;"
            "   background: transparent;"
            "}";
    }

    this->setStyleSheet(style);
}

bool SessionFriendItem::isSelected()
{
    return selected==true;
}

void SessionFriendItem::setSelected(bool se)
{
    selected=se;
}

SessionItem::SessionItem(SessionFriendArea *owner, QPushButton *avator, QString name, QString text, QWidget *parent)
    :SessionFriendItem(owner,avator,name)
{
    //进行我们的会话label的构建
    usernameLabel=new QLabel();
    usernameLabel->setText(text);
    usernameLabel->setStyleSheet(
        "QLabel {"
        "   color: rgb(170,170,170);"     // 消息颜色
        "   font-size: 12px;"
        "   background: transparent;"
        "   padding: 0px;"
        "   margin-top: 2px;"
        "}"
        );
    showname->setStyleSheet(
        "QLabel {"
        "   color: rgb(240,240,240);"     // 更亮的用户名
        "   font-size: 14px;"
        "   font-weight: bold;"           // 用户名加粗
        "   background: transparent;"
        "   padding: 0px;"
        "}"
        );

    //加入布局管理器中
    this->layout->addWidget(usernameLabel,1,2,1,8);
}

void SessionItem::active()
{
    //实现具体的点击逻辑
    //todo
    LOG()<<"进入了会话点击逻辑";
}

FriendItem::FriendItem(SessionFriendArea *owner, QPushButton *avator, QString name, QString signature, QWidget *parent)
    :SessionFriendItem(owner,avator,name)
{
    signatureLabel=new QLabel();
    signatureLabel->setText(signature);
    signatureLabel->setStyleSheet(
        "QLabel {"
        "   color: rgb(160,200,120);"     // 绿色个性签名
        "   font-size: 12px;"
        "   font-style: italic;"          // 斜体
        "   background: transparent;"
        "   padding: 0px;"
        "   margin-top: 2px;"
        "}"
        );

    // 优化用户名标签
    showname->setStyleSheet(
        "QLabel {"
        "   color: rgb(240,240,240);"
        "   font-size: 14px;"
        "   font-weight: bold;"
        "   background: transparent;"
        "   padding: 0px;"
        "}"
        );
    this->layout->addWidget(signatureLabel,1,2,1,8);

}

void FriendItem::active()
{
    //好友列表的实现逻辑
    LOG()<<"进入了好友元素点击逻辑";
}

ApplyItem::ApplyItem(SessionFriendArea *owner, QPushButton *avator, QString name, QWidget *parent)
    :SessionFriendItem(owner,avator,name)
{
    //设置同意按钮
    agreeBtn=new QPushButton();
    agreeBtn->setText("同意");

    //设置不同意按钮
    disagreeBtn=new QPushButton();
    disagreeBtn->setText("不同意");


    agreeBtn->setStyleSheet(
        "QPushButton {"
        "   background-color: rgb(0, 122, 255);"
        "   color: white;"
        "   border: none;"
        "   border-radius: 4px;"
        "   font-size: 12px;"
        "   font-weight: bold;"
        "   padding: 4px 8px;"
        "}"
        "QPushButton:hover {"
        "   background-color: rgb(0, 140, 255);"
        "}"
        "QPushButton:pressed {"
        "   background-color: rgb(0, 100, 220);"
        "}"
        );

    disagreeBtn->setStyleSheet(
        "QPushButton {"
        "   background-color: rgb(80, 80, 80);"
        "   color: rgb(220,220,220);"
        "   border: 1px solid rgb(100,100,100);"
        "   border-radius: 4px;"
        "   font-size: 12px;"
        "   font-weight: bold;"
        "   padding: 4px 8px;"
        "}"
        "QPushButton:hover {"
        "   background-color: rgb(100, 100, 100);"
        "}"
        "QPushButton:pressed {"
        "   background-color: rgb(60, 60, 60);"
        "}"
        );

    // 设置按钮固定尺寸
    agreeBtn->setFixedSize(50, 25);
    disagreeBtn->setFixedSize(50, 25);

    // 添加按钮信号连接
    connect(agreeBtn, &QPushButton::clicked, this, [this]() {
        LOG() << "同意好友申请:" << _username;
        // TODO: 实现同意逻辑
    });

    connect(disagreeBtn, &QPushButton::clicked, this, [this]() {
        LOG() << "拒绝好友申请:" << _username;
        // TODO: 实现拒绝逻辑
    });

    layout->addWidget(agreeBtn,1,2,1,4);
    layout->addWidget(disagreeBtn,1,6,1,4);
}

void ApplyItem::active()
{
    LOG()<<"进入好友点击元素点击逻辑";
}
